package com.sgb.kjwl.view.ui.activity.enterprise;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;

import com.alibaba.fastjson.JSONObject;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityAddneedlistBinding;
import com.sgb.kjwl.databinding.ActivityNeeddetailBinding;
import com.sgb.kjwl.model.entity.enterprise.NeedListItemEntity;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.DialogHelper;
import com.sgb.kjwl.viewmodel.enterprise.AddNeedDetailModel;

import java.io.Serializable;

public class AddNeedDetailActivity extends AppActivity {


    private AddNeedDetailModel mModel;
    private ActivityNeeddetailBinding mBingding;
    public static final int REQUEST_CODE = 0x112;


    @Override
    protected void initView() {
        super.initView();

        mModel = new AddNeedDetailModel(getApplication());
        mBingding = DataBindingUtil.setContentView(this, R.layout.activity_needdetail);
        mBingding.setVariable(BR.mdetailmodel,mModel);
        mBingding.setLifecycleOwner(this);

        com.swgk.core.dialog.DialogHelper.showProgressDialog(AddNeedDetailActivity.this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                com.swgk.core.dialog.DialogHelper.dismissProgressDialog();
                mModel.data.postValue((NeedListItemEntity)getIntent().getSerializableExtra("data"));
                mBingding.rlView.setVisibility(View.VISIBLE);
                mBingding.llBottom.setVisibility(View.VISIBLE);
            }
        },500);

        mModel.data.observe(this, new Observer<NeedListItemEntity>() {
            @Override
            public void onChanged(NeedListItemEntity needListItemEntity) {
                mBingding.tvPantType.setText(TextUtils.isEmpty(needListItemEntity.getPatentStatus())?"/":needListItemEntity.getPatentStatus().indexOf("0")>-1?needListItemEntity.getPatentStatus().indexOf("1")>-1?"授权已下证，授权未下证":"授权未下证":"授权已下证");
                mBingding.tvBanquanType.setText(TextUtils.isEmpty(needListItemEntity.getCopyrightAuthorization())?"/":needListItemEntity.getCopyrightAuthorization().indexOf("1")>-1?needListItemEntity.getCopyrightAuthorization().indexOf("2")>-1?"独家授权，非独家授权":"独家授权":"非独家授权");
                mBingding.tvBaosui.setText(TextUtils.isEmpty(needListItemEntity.getTaxReturns())?"/":needListItemEntity.getTaxReturns().indexOf("0")>-1?needListItemEntity.getTaxReturns().indexOf("1")>-1?"正常报税，不正常报税":"不正常报税":"正常报税");
  //              mBingding.tvServicePrincipal.setText(TextUtils.isEmpty(needListItemEntity.getServicePrincipal())?"/":needListItemEntity.getServicePrincipal().equals("1")?needListItemEntity.getServicePrincipal().equals("2")?"企业，团队":"企业":"团队");
                if(!TextUtils.isEmpty(needListItemEntity.getTaxScale())){
                    String data = "";
                    if(needListItemEntity.getTaxScale().indexOf(",")>-1) {
                        String[] ids = needListItemEntity.getTaxScale().split(",");
                        for (String id : ids) {
                            if(!TextUtils.isEmpty(gettype(id))) {
                                data = data + gettype(id) + ",";
                            }
                        }
                    }else{
                        data = gettype(needListItemEntity.getTaxScale())+",";
                    }
                    if(!TextUtils.isEmpty(data)){
                        mBingding.tvNashui.setText(data.substring(0,data.length()-1));
                    }
                }
                StringBuilder builder = new StringBuilder();
                if(!TextUtils.isEmpty(needListItemEntity.getCategoryFirstName())){

                    builder.append(needListItemEntity.getCategoryFirstName());
                }
                if(!TextUtils.isEmpty(needListItemEntity.getCategorySecondName())){

                    builder.append("-").append(needListItemEntity.getCategorySecondName());
                }
                if(!TextUtils.isEmpty(needListItemEntity.getCategoryThirdName())){

                    builder.append("-").append(needListItemEntity.getCategoryThirdName());
                }
                if(!TextUtils.isEmpty(needListItemEntity.getServicePrincipal())){
                    StringBuilder data= new StringBuilder();
                    if(needListItemEntity.getServicePrincipal().indexOf("1")>-1){
                        data.append("企业");
                        if(needListItemEntity.getServicePrincipal().indexOf("2")>-1){
                            data.append(",").append("团队");
                        }
                    }else
                    if(needListItemEntity.getServicePrincipal().indexOf("2")>-1){
                        data.append("团队");
                    }
                    mBingding.tvServicePrincipal.setText(data.toString());
                }
                mBingding.tvServiceType.setText(builder.toString());

                if(!TextUtils.isEmpty(needListItemEntity.getAssetsOldNew())){
                    mBingding.linOldnew.setVisibility(View.VISIBLE);

                }else{
                    mBingding.linOldnew.setVisibility(View.GONE);
                }

                if(!TextUtils.isEmpty(needListItemEntity.getRegisteredAddr())){

                    try {
                        StringBuilder stringBuilder = new StringBuilder();
                        JSONObject jsonObject = JSONObject.parseObject(needListItemEntity.getRegisteredAddr());

                        if(jsonObject.get("provinceName")!=null){
                            stringBuilder.append(jsonObject.getString("provinceName"));
                        }
                        if(jsonObject.get("cityName")!=null){
                            stringBuilder.append("-").append( jsonObject.getString("cityName"));
                        }
                        if(jsonObject.get("areaName")!=null){
                            stringBuilder.append("-").append(jsonObject.getString("areaName"));
                        }
                        mBingding.tvRegisterPath.setText(stringBuilder.toString());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }else{
                    mBingding.tvRegisterPath.setText("/");
                }
                if(!TextUtils.isEmpty(needListItemEntity.getEnterpriseTypeId())){
                    switch (Integer.parseInt(needListItemEntity.getEnterpriseTypeId())){
                        case 0:
                            mBingding.tvEnterpriseName.setText("有限责任公司");
                            break;
                        case 1:
                            mBingding.tvEnterpriseName.setText("个人独资公司");
                            break;
                        case 2:
                            mBingding.tvEnterpriseName.setText("港澳台公司");
                            break;

                        case 3:
                            mBingding.tvEnterpriseName.setText("集团公司");
                            break;
                    }

                }

            }
        });

        mBingding.tvOldnew.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if( !TextUtils.isEmpty(mBingding.tvOldnew.getText().toString())) {
                    int with_view = mBingding.tvOldnew.getWidth() - mBingding.tvOldnew.getPaddingLeft() - mBingding.tvOldnew.getPaddingRight();
                    Paint paint = mBingding.tvOldnew.getPaint();
                    float width_text = paint.measureText(mBingding.tvOldnew.getText().toString());
                    if (width_text > with_view) {
                        mBingding.imageMore.setVisibility(View.VISIBLE);
                        mModel.gotomore = true;
                    } else {
                        mBingding.imageMore.setVisibility(View.GONE);
                        mModel.gotomore = false;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mBingding.tvBianji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mintent = new Intent();
                switch (mModel.data.getValue().getRequirementTypes()){
                    case 1:
                        mintent.setClass(AddNeedDetailActivity.this,AddNeed_TrandActivity.class);
                        break;
                    case 2:
                        mintent.setClass(AddNeedDetailActivity.this,AddNeed_PatentActivity.class);
                        break;
                    case 3:
                        mintent.setClass(AddNeedDetailActivity.this,AddNeed_CopyRightActivity.class);
                        break;
                    case 4:
                        mintent.setClass(AddNeedDetailActivity.this,AddNeed_CompanyActivity.class);
                        break;
                    case 5:
                        mintent.setClass(AddNeedDetailActivity.this,AddNeed_ServiceActivity.class);
                        break;
                    case 6:
                        mintent.setClass(AddNeedDetailActivity.this,AddNeed_AssetsActivity.class);
                        break;
                }
                mintent.putExtra("data",(Serializable)mModel.data.getValue());
                startActivityForResult(mintent,REQUEST_CODE);

            }
        });

//        mBingding.linOldnew.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if(mModel.gotomore&&!TextUtils.isEmpty(mBingding.getMdetailmodel().data.getValue().getOldNewName())){
//                    AddNeedDetailShowMoreActivity.startactivity(AddNeedDetailActivity.this,mBingding.getMdetailmodel().data.getValue().getOldNewName());
//                }
//            }
//        });


        mBingding.tvType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogHelper.showDialog(AddNeedDetailActivity.this, "温馨提示", mModel.data.getValue().getDemandStatus().equals("1") ? "是否确认下架？" : "是否确认上架？", "确定", new DialogFunction() {
                    @Override
                    public void normalRoundDialog() {
                        mModel.updateType();
                    }
                });
            }
        });

        mBingding.tvDelete.setOnClickListener((view)->{
            DialogHelper.showDialog(AddNeedDetailActivity.this, "温馨提示", "是否确定删除？", "确定", new DialogFunction() {
                @Override
                public void normalRoundDialog() {
                    mModel.detletdata();
                }
            });
        });
        mModel.show_loading.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                switch (integer){
                    case 1:
                        com.swgk.core.dialog.DialogHelper.showProgressDialog(AddNeedDetailActivity.this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                        break;
                    case 2:
                        com.swgk.core.dialog.DialogHelper.dismissProgressDialog();
                        break;
                    case 3:
                        com.swgk.core.dialog.DialogHelper.dismissProgressDialog();
                        finish();
                        break;
                }
            }
        });


    }



    private String gettype(String s){
        String m = "";
        if(!TextUtils.isEmpty(s)){
            if(s.equals("0")){
                m = "小规模";
            }else if(s.equals("1")){
                m = "一般纳税人";
            }else if(s.equals("2")){
                m = "出口纳税人";
            }else if(s.equals("3")){
                m = "未核税";
            }
        }
        return m;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode ==REQUEST_CODE){
            finish();
            mModel.data.postValue((NeedListItemEntity)data.getSerializableExtra("data"));
        }
    }

    public static  void startactivity(Context mContext, NeedListItemEntity data){
        Intent mIntent = new Intent(mContext,AddNeedDetailActivity.class);
        mIntent.putExtra("data",(Serializable) data);
        mContext.startActivity(mIntent);
    }
}
